<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div id="app">
    <com1></com1>
    <com2></com2>

  </div>
  <script src="./node_modules/vue/dist/vue.js"></script>
  <script src="./node_modules/vuex/dist/vuex.js"></script>
  <script>
    // 假设现在store中有一个状态是依赖别的状态的改变而改变的，这时候可以使用getter

    let store = new Vuex.Store({
      state: {
        count: 0,
        num: 1,
      },
      getters: {
        sex(state) {
          // 这里的state就是store中的状态
          return state.count % 2 === 0 ? '男' : '女';
        }
      },
      mutations: {
        increment(state) {
          state.count++
        }
      }
    });

    const com1 = {
      data() {
        return {

        }
      },
      template: `<div @click="fn">组件一{{count}}</div>`,
      computed: {
        ss() { },
        ...Vuex.mapState(['count', 'num'])
      },
      methods: {
        fn() {
          this.$store.commit('increment');
        }
      }
    };
    const com2 = {
      data() {
        return {

        }
      },
      computed:{
        // ...Vuex.mapGetters(['sex'])
        ...Vuex.mapGetters({
          ss:'sex' // 可以给store中的状态换个名字,
        })
      },
      template: `<div>组件二{{this.$store.state.count}}{{ss}}</div>`,
    };

    let vm = new Vue({
      el: '#app',
      data: {

      },
      components: {
        com1,
        com2
      },
      store
    });

  </script>
</body>

</html>